/**
 * 全局清扫者
 */

let baser = require("./baser");

// 返回baser的内部生命函数
module.exports = function (fromroomname, toroomname) {
    return baser(
        /**
         * 准备函数，去往房间，卸货
         * @param {Creep} creep
         */
        function (creep) {
            if (creep.room.name != fromroomname) {
                creep.actionToRoom(fromroomname);
                return 1;
            }
            // 空了开始清扫
            if (creep.store[RESOURCE_ENERGY] < 5) {
                // 如果有其他资源
                if (creep.store[RESOURCE_ENERGY] +
                    creep.store.getFreeCapacity() != creep.store.getCapacity()) {
                    creep.actionStoreOthers();
                    return 1;
                }
                return 2;
            }
            // 继续卸货
            if (creep.actionStore()) {
                return 3;
            }
            return 1;
        },
        /**
         * 工作函数，去往房间，清扫
         * @param {Creep} creep
         */
        function (creep) {
            if (creep.room.name != toroomname) {
                creep.actionToRoom(toroomname);
                return 2;
            }
            // 满了开始卸货
            if (creep.store.getFreeCapacity() < 5) {
                return 1;
            }
            // 继续清扫
            if (creep.actionClean()) {
                return 3;
            }
            return 2;
        },
        /**
         * 结束函数，闲逛或其它
         * @param {Creep} creep
         */
        function (creep) {
            // 空了开始清扫
            if (creep.store[RESOURCE_ENERGY] < 5) {
                return 2;
            }
            if (creep.actionStore() == 1) {
                return 3;
            }
            return 1;
        });
}